package com.zhijian.medical.entity.vo.response;

import com.alibaba.fastjson.JSONObject;
import com.zhijian.medical.enums.diagnose.report.ReportEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 诊断-结构化报告 回显vo
 *
 * @author auto
 * @version 1.0
 * @date 2024-11-29 17:36:35
 */
@Data
@Api(tags = "诊断-结构化报告回显vo")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Component
public class RoutinePatientDiagnoseReportVo implements Serializable {


    private static final long serialVersionUID = -1;

    /**
     *
     */
    @ApiModelProperty("")
    private Long id;

    /**
     * 病理ID
     */
    @ApiModelProperty("病理ID")
    private Long patientId;

    /**
     * 诊断ID
     */
    @ApiModelProperty("诊断ID")
    private Long diagnoseId;

    /**
     * 报告类型：根据代码枚举
     */
    @ApiModelProperty("报告类型：根据代码枚举")
    private Integer type;

    /**
     * 报告类型：根据代码枚举
     */
    @ApiModelProperty("是否添加至病理诊断：0否，1是")
    private Integer isSave;

    /**
     * 报告类容
     */
    @ApiModelProperty("报告类容字符串")
    private String data;

    @ApiModelProperty("报告类容对象")
    private JSONObject dataJson;

    @ApiModelProperty("格式化富文本")
    private String text;

    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    private String createTime;

    /**
     * 修改时间
     */
    @ApiModelProperty("修改时间")
    private String updateTime;

    public void setData(String data) {
        this.data = data;

        this.dataJson = JSONObject.parseObject(data);

        ReportEnum.TypeEnum typeEnum = ReportEnum.TypeEnum.valueOfKey(this.type);
        if (typeEnum.equals(ReportEnum.TypeEnum.ESOPHAGUS)) {
            this.text = this.getEsophagusText(data);
        } else if (typeEnum.equals(ReportEnum.TypeEnum.STOMACH)) {
            this.text = this.getStomachText(data);
        } else {
            this.text = "";
        }


    }

    private String getEsophagusText(String text) {
        StringBuilder dataStr = new StringBuilder();

        try {
            cn.hutool.json.JSONObject jsonObject = new cn.hutool.json.JSONObject(text);

            // 转换为 Map
            Map<String, Object> map = jsonObject.toBean(Map.class);


            dataStr.append("（术前辅助治疗）：").append(map.get("subsidiary").toString()).append("\n\n");


            //标本循环
            List<Map<String, Object>> sampleList = (List<Map<String, Object>>) map.get("sampleList");
            for (int i = 0; i < sampleList.size(); i++) {
                dataStr.append("【标本").append(i + 1).append("信息】").append("\n");
                dataStr.append("  【基本诊断】\n");
                dataStr.append(" （标本名称）：").append(sampleList.get(i).get("sampleName")).append("\n");
                dataStr.append(" （肿瘤个数）：").append(sampleList.get(i).get("tumorNumber")).append("\n");

                List<Map<String, Object>> focusList = (List<Map<String, Object>>) sampleList.get(i).get("focusList");
                for (int i1 = 0; i1 < focusList.size(); i1++) {
                    dataStr.append(" 【病灶").append(i + 1).append("】").append("\n");
                    dataStr.append("  （肿瘤部位）：").append(focusList.get(i1).get("tumorSite"));
                    dataStr.append("  （肿瘤大小）：").append(focusList.get(i1).get("tumorSizeLong")).append(focusList.get(i1).get("tumorSizeWide")).append(focusList.get(i1).get("tumorSizeHigh"));
                    dataStr.append("  （巨检分型）：").append(focusList.get(i1).get("macrotyping")).append("\n");

                    List<Map<String, Object>> organizationalTypeList = (List<Map<String, Object>>) focusList.get(i1).get("organizationalTypeList");
                    for (int i2 = 0; i2 < organizationalTypeList.size(); i2++) {
                        dataStr.append("    （组织学分型）：").append(organizationalTypeList.get(i2).get("organizationalType")).append("；");
                        dataStr.append("（诊断编码 ICD）：").append(organizationalTypeList.get(i2).get("diagnosticCodeICD")).append("；");
                        dataStr.append("（O）：").append(organizationalTypeList.get(i2).get("diagnosticCodeO")).append("；");
                        dataStr.append("（百分比）：").append(organizationalTypeList.get(i2).get("percent")).append("\n");
                    }
                    dataStr.append("  （组织学分级）：").append(focusList.get(i1).get("histologicalGrade")).append("\n");
                    dataStr.append("  （肿瘤浸润程度）：").append(focusList.get(i1).get("tumorInfiltration"));
                    dataStr.append("  （累及胃食管交界）：").append(focusList.get(i1).get("gastroesophagus"));
                    dataStr.append("  （脉管癌栓）：").append(focusList.get(i1).get("vascularCancer"));
                    dataStr.append("  （神经侵犯）：").append(focusList.get(i1).get("neuroaggression")).append("\n");

                    List<Map<String, Object>> otherOrganizationList = (List<Map<String, Object>>) focusList.get(i1).get("otherOrganizationList");
                    for (int i3 = 0; i3 < otherOrganizationList.size(); i3++) {
                        dataStr.append("  （其他组织）：").append(otherOrganizationList.get(i3).get("otherOrganization"));
                        dataStr.append("  （是否侵犯）：").append(otherOrganizationList.get(i3).get("isNeuroaggression")).append("\n");
                    }
                }


                dataStr.append("【切缘累及情况】\n\n");

                dataStr.append("  （两端切缘）：").append(sampleList.get(i).get("betweenEdge")).append("\n");
                dataStr.append("  （上切缘）：").append(sampleList.get(i).get("upEdge")).append("\n");
                dataStr.append("  （下切缘）：").append(sampleList.get(i).get("underEdge")).append("\n");
                dataStr.append("  （环周切缘）：").append(sampleList.get(i).get("circumferentialEdge")).append("\n");
                dataStr.append("  （吻合圈）：").append(sampleList.get(i).get("anastomosisRing")).append("\n");

                List<Map<String, Object>> edgeList = (List<Map<String, Object>>) sampleList.get(i).get("edgeList");
                for (int i4 = 0; i4 < edgeList.size(); i4++) {
                    dataStr.append(edgeList.get(i4).get("edgeName")).append("：").append(edgeList.get(i4).get("edgeContent")).append("\n");
                }

                // 周围黏膜情况
                dataStr.append("  （周围黏膜情况）\n");
                dataStr.append("  （周围食管黏膜）：").append(sampleList.get(i).get("esophagealMucosa"));
                dataStr.append("  （周围食管黏膜）：").append(sampleList.get(i).get("gastricMucosa")).append("\n\n");

            }


            // 其他病理结果
            dataStr.append("【淋巴结】\n");
            List<Map<String, Object>> lymphNodeList = (List<Map<String, Object>>) map.get("lymphNodeList");
            for (int i = 0; i < lymphNodeList.size(); i++) {
                dataStr.append("  （").append(lymphNodeList.get(i).get("name")).append("）：");
                dataStr.append(lymphNodeList.get(i).get("transferNumber")).append("，");
                dataStr.append(lymphNodeList.get(i).get("totalNumber")).append("，");
                dataStr.append(lymphNodeList.get(i).get("symptom")).append("，");
                dataStr.append("备注：").append(lymphNodeList.get(i).get("remark")).append("\n");
            }

            //转移个数/总个数
            dataStr.append("转移个数/总个数：").append(map.get("transferNumber")).append("/").append(map.get("totalNumber")).append("\n\n");

            dataStr.append("【远处转移】").append("\n");
            dataStr.append("  ").append(map.get("distantMetastasis")).append("\n\n");

            dataStr.append("【免疫组化】\n");
            List<Map<String, Object>> immunohistochemistryList = (List<Map<String, Object>>) map.get("immunohistochemistryList");
            for (int i = 0; i < immunohistochemistryList.size(); i++) {
                dataStr.append("  蜡块号：").append(immunohistochemistryList.get(i).get("waxNumber")).append("；");
                dataStr.append("标记物：").append(immunohistochemistryList.get(i).get("marker")).append("；");
                dataStr.append("肿瘤细胞表达：").append(immunohistochemistryList.get(i).get("tumorExpressionSymbol")).append("  ").append(immunohistochemistryList.get(i).get("tumorExpressionPercent")).append("；");
                dataStr.append("其他表达：").append(immunohistochemistryList.get(i).get("otherExpression")).append("；");
                dataStr.append("分析结果：").append(immunohistochemistryList.get(i).get("analysisResultSymbol")).append("  ").append(immunohistochemistryList.get(i).get("analysisResultPercent")).append("\n");
            }

            //原位杂交 situHybridizationList
            dataStr.append("【原位杂交】\n");
            List<Map<String, Object>> situHybridizationList = (List<Map<String, Object>>) map.get("situHybridizationList");
            for (int i = 0; i < situHybridizationList.size(); i++) {
                dataStr.append("  蜡块号：").append(situHybridizationList.get(i).get("waxNumber")).append("；");
                dataStr.append("标记物：").append(situHybridizationList.get(i).get("marker")).append("；");
                dataStr.append("分析结果：").append(situHybridizationList.get(i).get("analysisResult")).append("；");
                dataStr.append("备注：").append(situHybridizationList.get(i).get("remark")).append("\n");
            }


            //特殊染色 specialDyeingList
            dataStr.append("【特殊染色】\n");
            List<Map<String, Object>> specialDyeingList = (List<Map<String, Object>>) map.get("specialDyeingList");
            for (int i = 0; i < specialDyeingList.size(); i++) {
                dataStr.append("  蜡块号：").append(specialDyeingList.get(i).get("waxNumber")).append("；");
                dataStr.append("标记物：").append(specialDyeingList.get(i).get("marker")).append("；");
                dataStr.append("分析结果：").append(specialDyeingList.get(i).get("analysisResult")).append("；");
                dataStr.append("备注：").append(specialDyeingList.get(i).get("remark")).append("\n");
            }

            // 其他送检组织 otherOrganizationList
            dataStr.append("【其他送检组织】\n");
            List<Map<String, Object>> otherOrganizationList = (List<Map<String, Object>>) map.get("otherOrganizationList");
            for (int i = 0; i < otherOrganizationList.size(); i++) {
                dataStr.append("  （其他送检组织）：").append(otherOrganizationList.get(i).get("otherOrganization")).append("\n");
                dataStr.append("  （组织描述）：").append(otherOrganizationList.get(i).get("organizationDescription")).append("\n");
            }

            // 备注
            dataStr.append("【备注】：").append(map.get("remark")).append("\n");
        } catch (Exception ignored) {
        }

        return dataStr.toString();
    }

    /**
     * 获取胃
     *
     * @param text
     * @return
     */
    private String getStomachText(String text) {

        StringBuilder dataStr = new StringBuilder();

        try {
            cn.hutool.json.JSONObject jsonObject = new cn.hutool.json.JSONObject(text);

            // 转换为 Map
            Map<String, Object> map = jsonObject.toBean(Map.class);


            dataStr.append("报告模版：胃癌        术前辅助治疗：").append(map.get("subsidiary").toString()).append("\n\n");


            //标本循环
            List<Map<String, Object>> sampleList = (List<Map<String, Object>>) map.get("sampleList");
            for (int i = 0; i < sampleList.size(); i++) {
                dataStr.append("标本").append(i + 1).append("\n");
                dataStr.append("基本诊断\n");
                dataStr.append("标本名称：").append(sampleList.get(i).get("sampleName")).append("                        *肿瘤个数:").append(sampleList.get(i).get("tumorNumber")).append("\n");

                List<Map<String, Object>> focusList = (List<Map<String, Object>>) sampleList.get(i).get("focusList");
                for (int i1 = 0; i1 < focusList.size(); i1++) {
                    dataStr.append("病灶").append(i1 + 1).append("\n");
                    dataStr.append("肿瘤部位：").append(focusList.get(i1).get("tumorSite"));
                    dataStr.append("      肿瘤大小：").append(focusList.get(i1).get("tumorSizeLong")).append(focusList.get(i1).get("tumorSizeWide")).append(focusList.get(i1).get("tumorSizeHigh"));
                    dataStr.append("      巨检分型：").append(focusList.get(i1).get("macrotyping")).append("\n");


                    List<Map<String, Object>> organizationalTypeList = (List<Map<String, Object>>) focusList.get(i1).get("organizationalTypeList");
                    for (int i2 = 0; i2 < organizationalTypeList.size(); i2++) {
                        dataStr.append("WHO组织学分型：").append(organizationalTypeList.get(i2).get("organizationalType"));
                        dataStr.append("  诊断编码");
                        dataStr.append("  ICD：").append(organizationalTypeList.get(i2).get("diagnosticCodeICD"));
                        dataStr.append("  O：").append(organizationalTypeList.get(i2).get("diagnosticCodeO"));
                        dataStr.append("  百分比：").append(organizationalTypeList.get(i2).get("percent")).append("\n");
                    }

                    dataStr.append("\nLauren分型:").append(focusList.get(i1).get("lauren"));
                    dataStr.append("Siewert分型:").append(focusList.get(i1).get("siewert")).append("\n");

                    dataStr.append("Ⅰ型：相当于远端食管腺癌，肿瘤中心位于食管胃交界部上 1~5cm 处。").append("\n");
                    dataStr.append("Ⅱ型：相当于贲门腺癌，肿瘤中心位于食管胃交界部上 1cm 至下 2cm 处。").append("\n");
                    dataStr.append("Ⅲ型：相当于贲门下腺癌，肿瘤中心位于食管胃交界部下 2~5cm 处。").append("\n\n");


                    dataStr.append("肿瘤级别：").append(focusList.get(i1).get("tumorGrade"));
                    dataStr.append("    粘液分型：").append(focusList.get(i1).get("mucousType"));
                    dataStr.append("    浸润深度：").append(focusList.get(i1).get("invasionDepth"));
                    dataStr.append("    累及远端食管：").append(focusList.get(i1).get("distalEsophagus").equals(true) ? "是" : "否").append("\n");
                    dataStr.append("浸润方式：").append(focusList.get(i1).get("InfiltrationMode"));
                    dataStr.append("    脉管癌栓：").append(focusList.get(i1).get("vascularCancer"));
                    dataStr.append("    神经侵犯：").append(focusList.get(i1).get("neuroaggression"));
                    dataStr.append("    蜡块号：").append(focusList.get(i1).get("waxNumber")).append("\n");
                }


                dataStr.append("\n切缘累及情况\n");

                dataStr.append("上切缘：").append(sampleList.get(i).get("upEdge"));
                dataStr.append("  最近距离为：").append(sampleList.get(i).get("upDistance"));
                dataStr.append("  下切缘：").append(sampleList.get(i).get("underEdge"));
                dataStr.append("  最近距离为：").append(sampleList.get(i).get("underDistance"));
                dataStr.append("  吻合圈：").append(sampleList.get(i).get("anastomosisRing"));
                dataStr.append("  周围胃黏膜：").append(String.join("、", (List<String>) sampleList.get(i).get("gastricMucosa")));
                dataStr.append("  网膜组织：").append("omentalTissue").append("\n");

                //edgeList
                List<Map<String, Object>> edgeList = (List<Map<String, Object>>) sampleList.get(i).get("edgeList");
                for (Map<String, Object> edgeItem : edgeList) {
                    dataStr.append(edgeItem.get("edgeName")).append("：").append(edgeItem.get("edgeContent")).append("\n");
                }

            }


            // 其他病理结果
            dataStr.append("\n其他病理结果\n");
            dataStr.append("淋巴结\n");
            List<Map<String, Object>> lymphNodeList = (List<Map<String, Object>>) map.get("lymphNodeList");
            for (int i = 0; i < lymphNodeList.size(); i++) {
                dataStr.append(lymphNodeList.get(i).get("number").toString());
                dataStr.append("    ").append(lymphNodeList.get(i).get("name"));
                dataStr.append("    ").append(lymphNodeList.get(i).get("transferNumber"));
                dataStr.append("    ").append(lymphNodeList.get(i).get("totalNumber"));
                dataStr.append("    ").append(lymphNodeList.get(i).get("symptom"));
                dataStr.append("    ").append(lymphNodeList.get(i).get("remark")).append("\n");
            }

            //转移个数/总个数
            dataStr.append("转移个数/总个数：").append(map.get("transferNumber")).append("/").append(map.get("totalNumber")).append("\n\n");

            dataStr.append("\n远处转移").append(map.get("distantMetastasis")).append("\n\n");

            dataStr.append("\n免疫组化\n");
            List<Map<String, Object>> immunohistochemistryList = (List<Map<String, Object>>) map.get("immunohistochemistryList");
            for (int i = 0; i < immunohistochemistryList.size(); i++) {
                dataStr.append("蜡块号：").append(immunohistochemistryList.get(i).get("waxNumber")).append("\n");
                dataStr.append("标记物：").append(immunohistochemistryList.get(i).get("marker")).append("\n");
                dataStr.append("肿瘤细胞表达：").append(immunohistochemistryList.get(i).get("tumorExpressionSymbol")).append("  ").append(immunohistochemistryList.get(i).get("tumorExpressionPercent")).append("\n");
                dataStr.append("其他表达：").append(immunohistochemistryList.get(i).get("otherExpression")).append("\n");
                dataStr.append("分析结果：").append(immunohistochemistryList.get(i).get("analysisResultSymbol")).append("  ").append(immunohistochemistryList.get(i).get("analysisResultPercent")).append("\n");
            }

            //原位杂交 situHybridizationList
            dataStr.append("\n原位杂交\n");
            List<Map<String, Object>> situHybridizationList = (List<Map<String, Object>>) map.get("situHybridizationList");
            for (int i = 0; i < situHybridizationList.size(); i++) {
                dataStr.append("蜡块号：").append(situHybridizationList.get(i).get("waxNumber")).append("\n");
                dataStr.append("标记物：").append(situHybridizationList.get(i).get("marker")).append("\n");
                dataStr.append("分析结果：").append(situHybridizationList.get(i).get("analysisResult")).append("\n");
                dataStr.append("备注：").append(situHybridizationList.get(i).get("remark")).append("\n");
            }


            //特殊染色 specialDyeingList
            dataStr.append("\n特殊染色\n");
            List<Map<String, Object>> specialDyeingList = (List<Map<String, Object>>) map.get("specialDyeingList");
            for (int i = 0; i < specialDyeingList.size(); i++) {
                dataStr.append("蜡块号：").append(specialDyeingList.get(i).get("waxNumber")).append("\n");
                dataStr.append("标记物：").append(specialDyeingList.get(i).get("marker")).append("\n");
                dataStr.append("分析结果：").append(specialDyeingList.get(i).get("analysisResult")).append("\n");
                dataStr.append("备注：").append(specialDyeingList.get(i).get("remark")).append("\n");
            }

            // 其他送检组织 otherOrganizationList
            dataStr.append("\n其他送检组织\n");
            List<Map<String, Object>> otherOrganizationList = (List<Map<String, Object>>) map.get("otherOrganizationList");
            for (int i = 0; i < otherOrganizationList.size(); i++) {
                dataStr.append("其他送检组织：").append(otherOrganizationList.get(i).get("otherOrganization")).append("\n");
                dataStr.append("组织描述：").append(otherOrganizationList.get(i).get("organizationDescription")).append("\n");
            }

            // 备注
            dataStr.append("\n备注：").append(map.get("remark")).append("\n");
        } catch (Exception ignored) {

        }


        return dataStr.toString();
    }
}